﻿// -----------------------------------------------------------------------
// <copyright file="CreateLinkCommandHandler.cs">
// Copyright (c) 2014 Andrey Veselov. All rights reserved.
// License:  Microsoft Public License (MS-PL)
// Contacts: http://andrey.moveax.com  andrey@moveax.com
// </copyright>
// -----------------------------------------------------------------------

using Moveax.DataAccess.Cqrs.Highway.EF.UnitTests.Model.Commands.Links;

namespace Moveax.DataAccess.Cqrs.Highway.EF.UnitTests.DataAccess.CommandHandlers.Links
{
	using System.Diagnostics.Contracts;
	using Moveax.DataAccess.Cqrs.Highway.EF.UnitTests.DataAccess.Database.Dto;

	internal class CreateLinkCommandHandler : ICommandHandlerSync<CreateLinkCommand>, ICommandHandlerTransactionSupport
	{
		private readonly IRepository _repository;
		private LinkDto _link;
		private CreateLinkCommand _command;

		public CreateLinkCommandHandler(IRepository repository)
		{
			Contract.Requires(repository != null);

			this._repository = repository;
		}

		public void Execute(CreateLinkCommand command)
		{
			this._command = command;

			this._link = new LinkDto() {
				Id = default(int),
				Title = command.Title,
				Url = command.Url,
				UserProfileId = command.UserId
			};

			this._repository.Add(this._link);
		}

		public void OnCommitted()
		{
			this._command.Id = this._link.Id;
		}
	}
}
